<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\card\model;

class CardValue extends \app\BaseModel
{
    protected $name = "longbing_card_value";
    protected static function init()
    {
    }
    public function getlist($where)
    {
        return $this->where($where)->order("update_time", "desc")->find();
    }
    public function bossGetAiValue($id, $uniacid)
    {
        $value = ["client" => 0, "charm" => 0, "interaction" => 0, "product" => 0, "website" => 0, "active" => 0];
        $check = $this->getlist(["staff_id" => $id]);
        if(!$check || $check && 86400 < time() - $check["update_time"]) {
            $colle = new Collection();
            $client = $colle->getlistCount(["status" => 1, "to_uid" => $id]);
            if(0 < $client) {
                $client -= 1;
            }
            $value["client"] = $client;
            $card_count = new CardCount();
            $list1 = $card_count->getCount(["sign" => "praise", "type" => 1, "to_uid" => $id]);
            $list2 = $card_count->getCount(["sign" => "praise", "type" => 3, "to_uid" => $id]);
            $list3 = $card_count->getCount(["sign" => "copy", "to_uid" => $id]);
            $count = $list1 + $list2 + $list3;
            $value["charm"] = $count;
            $card_message = new CardMessage();
            $list1 = $card_message->getCount(["user_id" => $id]);
            $list2 = $card_message->getCount(["target_id" => $id]);
            $list3 = $card_count->getCount(["sign" => "view", "to_uid" => $id]);
            $count = $list1 + $list2 + $list3;
            $value["interaction"] = $count;
            $extension = new CardExtension();
            $mark = new UserMark();
            $forward = new CardForward();
            $group = new \app\dynamic\model\CardShareGroup();
            $list1 = $extension->getCount(["user_id" => $id, "uniacid" => $uniacid]);
            $list2 = $mark->getCount(["staff_id" => $id, "uniacid" => $uniacid, "mark" => 2]);
            $list3 = $forward->forwardCount(["staff_id" => $id, "uniacid" => $uniacid, "type" => 2]);
            $where = [["user_id", "=", $id], ["uniacid", "=", $uniacid], ["view_goods", "<>", ""]];
            $list4 = $group->getCount($where);
            $count = $list1 + $list2 + $list3 + $list4;
            $value["product"] = $count;
            $list1 = $card_count->getCount(["sign" => "view", "type" => 6, "to_uid" => $id]);
            $list2 = $forward->forwardCount(["staff_id" => $id, "uniacid" => $uniacid, "type" => 4]);
            $count = $list1 + $list2;
            $value["website"] = $count;
            $follow = new UserFollow();
            $list1 = $card_message->getCount(["user_id" => $id]);
            $list2 = $card_message->getCount(["target_id" => $id]);
            $list3 = $follow->getCount(["staff_id" => $id]);
            $list4 = $mark->getCount(["staff_id" => $id]);
            $count = $list1 + $list2 + $list3 + $list4;
            $value["active"] = $count;
            $insertData = $value;
            $insertData["staff_id"] = $id;
            $time = time();
            $insertData["update_time"] = $time;
            $insertData["uniacid"] = $uniacid;
            if(!$check) {
                $insertData["create_time"] = $time;
                $this->createRow($insertData);
            } else {
                $insertData["update_time"] = $time;
                $this->upsave(["id" => $check["id"]], $insertData);
            }
        } else {
            $value = ["client" => $check["client"], "charm" => $check["charm"], "interaction" => $check["interaction"], "product" => $check["product"], "website" => $check["website"], "active" => $check["active"]];
        }
        $max = array_sum([$value["active"], $value["charm"], $value["client"], $value["interaction"], $value["product"], $value["website"]]);
        $web_name = tabbarName(4, $uniacid);
        $bb[] = $value["website"];
        $data = [["name" => "销售主动性值", "number" => $value["active"]], ["name" => !empty($web_name) ? $web_name . "推广度" : "官网推广度", "number" => $value["website"], $bb], ["name" => "产品推广值", "number" => $value["product"]], ["name" => "客户互动值", "number" => $value["interaction"]], ["name" => "获客能力值", "number" => $value["client"]], ["name" => "个人魅力值", "number" => $value["charm"]]];
        return ["max" => $max, "data" => $data];
    }
    public function bossAi($user_list, $uniacid)
    {
        $max = [0, 0, 0, 0, 0, 0];
        $staff_list = User::where([["uniacid", "=", $uniacid], ["is_staff", "=", 1]])->field(["id", "nickName"])->where("id", "in", $user_list)->select()->toArray();
        foreach ($staff_list as $k => $v) {
            $total = 0;
            $value = $this->bossGetAiValue($v["id"], $uniacid);
            foreach ($value["data"] as $k2 => $v2) {
                if($max[$k2] < $v2["number"]) {
                    $max[$k2] = $v2["number"];
                }
                $total += $v2["number"];
            }
            $staff_list[$k]["number"] = $value["data"];
        }
        $data = ["max_data" => $max, "max_number" => max($max)];
        return $data;
    }
}

?>